<div [@routerTransition]>
    <div class="m-subheader">
        <div class="d-flex align-items-center">
            <div class="mr-auto col-xs-6">
                <h3 class="m-subheader__title m-subheader__title--separator">
                    <span>{{l("Users")}}</span>
                </h3>
                <span class="m-section__sub">
                    {{l("UsersHeaderInfo")}}
                </span>
            </div>
            <div class="col-xs-6 text-right">
                <button (click)="exportToExcel()" class="btn btn-outline-success"><i class="fa fa-file-excel-o"></i> {{l("ExportToExcel")}}</button>
                <button (click)="createUser()" *ngIf="isGranted('Pages.Administration.Users.Create')" class="btn btn-primary"><i class="fa fa-plus"></i> {{l("CreateNewUser")}}</button>
            </div>
        </div>
    </div>
    <div class="m-content">
        <div class="m-portlet m-portlet--mobile">
            <div class="m-portlet__body">
                <form class="horizontal-form" autocomplete="off">
                    <div class="m-form m-form--label-align-right">
                        <div class="row align-items-center m--margin-bottom-10">
                            <div class="col-xl-12">
                                <div class="form-group m-form__group align-items-center">
                                    <div class="input-group">
                                        <input [(ngModel)]="filterText" name="filterText" autoFocus class="form-control m-input" [placeholder]="l('SearchWithThreeDot')" type="text">
                                        <span class="input-group-btn">
                                            <button (click)="getUsers()" class="btn btn-primary" type="submit"><i class="flaticon-search-1" [attr.aria-label]="l('Search')"></i></button>
                                        </span>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="row m--margin-bottom-10" [hidden]="!advancedFiltersAreShown">
                            <div [ngClass]="{'col-md-6': isGranted('Pages.Administration.Roles'), 'col-md-12':!isGranted('Pages.Administration.Roles')}">
                                <div class="form-group">
                                    <permission-combo [(selectedPermission)]="selectedPermission"></permission-combo>
                                </div>
                            </div>
                            <div class="col-md-6" *ngIf="isGranted('Pages.Administration.Roles')">
                                <div class="form-group">
                                    <role-combo [(selectedRole)]="role" emptyText="{{l('FilterByRole')}}"></role-combo>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="m-checkbox-list">
                                    <label for="UsersTable_OnlyLockedUsers" class="m-checkbox">
                                        <input id="UsersTable_OnlyLockedUsers" type="checkbox" name="OnlyLockedUsers" [(ngModel)]="onlyLockedUsers">
                                        {{l("OnlyLockedUsers")}}
                                        <span></span>
                                    </label>
                                </div>
                            </div>
                        </div>

                        <div class="row margin-bottom-10" *ngIf="advancedFiltersAreShown">
                            <div class="col-sm-12 text-right">
                                <button class="btn btn-metal" (click)="getUsers()"><i class="fa fa-refresh"></i> {{l("Refresh")}}</button>
                            </div>
                        </div>
                        <div class="row margin-bottom-10">
                            <div class="col-sm-12">
                                <span class="clickable-item text-muted" *ngIf="!advancedFiltersAreShown" (click)="advancedFiltersAreShown=!advancedFiltersAreShown"><i class="fa fa-angle-down"></i> {{l("ShowAdvancedFilters")}}</span>
                                <span class="clickable-item text-muted" *ngIf="advancedFiltersAreShown" (click)="advancedFiltersAreShown=!advancedFiltersAreShown"><i class="fa fa-angle-up"></i> {{l("HideAdvancedFilters")}}</span>
                            </div>
                        </div>
                    </div>
                </form>

                <div class="row align-items-center">
                    <!--<Primeng-TurboTable-Start>-->
                    <div class="primeng-datatable-container"
                         [busyIf]="primengTableHelper.isLoading">
                        <p-table #dataTable
                                 (onLazyLoad)="getUsers($event)"
                                 [value]="primengTableHelper.records"
                                 rows="{{primengTableHelper.defaultRecordsCountPerPage}}"
                                 [paginator]="false"
                                 [lazy]="true"
                                 [scrollable]="true"
                                 ScrollWidth="100%"
                                 [responsive]="primengTableHelper.isResponsive"
                                 [resizableColumns]="primengTableHelper.resizableColumns">
                            <ng-template pTemplate="header">
                                <tr>
                                    <th style="width: 130px" [hidden]="!isGrantedAny('Pages.Administration.Users.Impersonation', 'Pages.Administration.Users.Edit', 'Pages.Administration.Users.ChangePermissions', 'Pages.Administration.Users.Delete')">{{l('Actions')}}</th>
                                    <th style="width: 150px" pSortableColumn="username">
                                        {{l('UserName')}}
                                        <p-sortIcon field="username"></p-sortIcon>
                                    </th>
                                    <th style="width: 150px" pSortableColumn="name">
                                        {{l('Name')}}
                                        <p-sortIcon field="name"></p-sortIcon>
                                    </th>
                                    <th style="width: 150px" pSortableColumn="surname">
                                        {{l('Surname')}}
                                        <p-sortIcon field="surname"></p-sortIcon>
                                    </th>
                                    <th style="width: 150px">
                                        {{l('Roles')}}
                                    </th>
                                    <th style="width: 250px" pSortableColumn="emailAddress">
                                        {{l('EmailAddress')}}
                                        <p-sortIcon field="emailAddress"></p-sortIcon>
                                    </th>
                                    <th style="width: 200px" pSortableColumn="isEmailConfirmed">
                                        {{l('EmailConfirm')}}
                                        <p-sortIcon field="isEmailConfirmed"></p-sortIcon>
                                    </th>
                                    <th style="width: 100px" pSortableColumn="isActive">
                                        {{l('Active')}}
                                        <p-sortIcon field="isActive"></p-sortIcon>
                                    </th>
                                    <th style="width: 200px" pSortableColumn="lastLoginTime">
                                        {{l('LastLoginTime')}}
                                        <p-sortIcon field="lastLoginTime"></p-sortIcon>
                                    </th>
                                    <th style="width: 200px" pSortableColumn="creationTime">
                                        {{l('CreationTime')}}
                                        <p-sortIcon field="creationTime"></p-sortIcon>
                                    </th>
                                </tr>
                            </ng-template>
                            <ng-template pTemplate="body" let-record="$implicit">
                                <tr>
                                    <td style="width: 130px"
                                        [hidden]="!isGrantedAny('Pages.Administration.Users.Impersonation', 'Pages.Administration.Users.Edit', 'Pages.Administration.Users.ChangePermissions', 'Pages.Administration.Users.Delete')">
                                        <div class="btn-group dropdown" normalizePosition>
                                            <button class="dropdown-toggle btn btn-sm btn-primary"
                                                    data-toggle="dropdown"
                                                    aria-haspopup="true"
                                                    aria-expanded="false">
                                                <i class="fa fa-cog"></i><span class="caret"></span> {{l("Actions")}}
                                            </button>
                                            <ul class="dropdown-menu">
                                                <li>
                                                    <a *ngIf="permission.isGranted('Pages.Administration.Users.Impersonation') && record.id !== appSession.userId"
                                                       (click)="_impersonationService.impersonate(record.id, appSession.tenantId)">{{l('LoginAsThisUser')}}</a>
                                                </li>
                                                <li>
                                                    <a *ngIf="permission.isGranted('Pages.Administration.Users.Edit')"
                                                       (click)="createOrEditUserModal.show(record.id)">{{l('Edit')}}</a>
                                                </li>
                                                <li>
                                                    <a *ngIf="permission.isGranted('Pages.Administration.Users.ChangePermissions')"
                                                       (click)="editUserPermissionsModal.show(record.id, record.userName)">{{l('Permissions')}}</a>
                                                </li>
                                                <li>
                                                    <a *ngIf="permission.isGranted('Pages.Administration.Users.ChangePermissions')"
                                                       (click)="unlockUser(record)">{{l('Unlock')}}</a>
                                                </li>
                                                <li>
                                                    <a *ngIf="permission.isGranted('Pages.Administration.Users.Delete')"
                                                       (click)="deleteUser(record)">{{l('Delete')}}</a>
                                                </li>
                                            </ul>
                                        </div>
                                    </td>
                                    <td style="width: 150px">
                                        <span class="ui-column-title"> {{l('UserName')}}</span>
                                        {{record.userName}}
                                    </td>
                                    <td style="width: 150px">
                                        <span class="ui-column-title"> {{l('Name')}}</span>
                                        {{record.name}}
                                    </td>
                                    <td style="width: 150px">
                                        <span class="ui-column-title"> {{l('Surname')}}</span>
                                        {{record.surname}}
                                    </td>
                                    <td style="width: 150px">
                                        <span class="ui-column-title"> {{l('Roles')}}</span>
                                        {{getRolesAsString(record.roles)}}
                                    </td>
                                    <td style="width: 250px">
                                        <span class="ui-column-title"> {{l('EmailAddress')}}</span>
                                        {{record.emailAddress}}
                                    </td>
                                    <td style="width: 200px">
                                        <span class="ui-column-title"> {{l('EmailConfirm')}}</span>
                                        <span class="label label-success" *ngIf="record.isEmailConfirmed">{{l('Yes')}}</span>
                                        <span class="label label-default" *ngIf="!record.isEmailConfirmed">{{l('No')}}</span>
                                    </td>
                                    <td style="width: 100px">
                                        <span class="ui-column-title"> {{l('Active')}}</span>
                                        <span class="label label-success" *ngIf="record.isActive">{{l('Yes')}}</span>
                                        <span class="label label-default" *ngIf="!record.isActive">{{l('No')}}</span>
                                    </td>
                                    <td style="width: 200px">
                                        <span class="ui-column-title"> {{l('LastLoginTime')}}</span>
                                        <span *ngIf="record.lastLoginTime">{{record.lastLoginTime | momentFormat:'L'}}</span>
                                        <span *ngIf="!record.lastLoginTime">-</span>
                                    </td>
                                    <td style="width: 200px">
                                        <span class="ui-column-title"> {{l('CreationTime')}}</span>
                                        {{record.creationTime | momentFormat:'L'}}
                                    </td>
                                </tr>
                            </ng-template>
                        </p-table>
                        <div class="primeng-no-data" *ngIf="primengTableHelper.totalRecordsCount == 0">
                            {{l('NoData')}}
                        </div>
                        <div class="primeng-paging-container">
                            <p-paginator rows="{{primengTableHelper.defaultRecordsCountPerPage}}"
                                         #paginator
                                         (onPageChange)="getUsers($event)"
                                         [totalRecords]="primengTableHelper.totalRecordsCount"
                                         [rowsPerPageOptions]="primengTableHelper.predefinedRecordsCountPerPage">
                            </p-paginator>
                            <span class="total-records-count">
                                {{l('TotalRecordsCount', primengTableHelper.totalRecordsCount)}}
                            </span>
                        </div>
                    </div>
                    <!--<Primeng-TurboTable-End>-->
                </div>
            </div>
        </div>
    </div>

    <createOrEditUserModal #createOrEditUserModal (modalSave)="getUsers()"></createOrEditUserModal>

    <editUserPermissionsModal #editUserPermissionsModal></editUserPermissionsModal>
</div>
